package com.sys.web.head.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.servlet.BaseServlet;
import com.sys.common.util.PageInfoUtil;
import com.sys.users.pojo.TUsers;
import com.sys.wares.pojo.TSugg;
import com.sys.wares.pojo.TSuggEnum;
import com.sys.wares.service.IGoodsDbService;
import com.sys.wares.service.impl.GoodsDbServiceImpl;

/**
 * 商品前台操作
 * 
 * @author 杨志超
 */
@WebServlet(urlPatterns = "/head/GoodsHeadServlet")
public class WaresHeadServlet extends BaseServlet
{
	private IGoodsDbService goodsDbService;
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.goodsDbService = new GoodsDbServiceImpl();
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("commentList".equalsIgnoreCase(method))
		{
			/* commentList */
			commentList(request, response);
		} else if ("commentInsert".equalsIgnoreCase(method))
		{
			/* commentInsert */
			commentInsert(request, response);
		}else if ("commentInsertSubmit".equalsIgnoreCase(method))
		{
			/* commentInsertSubmit */
			commentInsertSubmit(request, response);
		} else if ("commentUpdate".equalsIgnoreCase(method))
		{
			/* commentUpdate */
			commentUpdate(request, response);
		} 
	}
	
	/**
	 * 评论列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String commentList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		TUsers users = (TUsers) this.findObjfromSession(request, "users");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		condMap.put("usersId", users.getId());
		condMap.put("status", TSuggEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<TSugg> apiResponse = this.goodsDbService.findCondListCommentService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/goods/commentList";
	}

	/**
	 * 打开评论页面
	 * @param request
	 * @param response
	 */
	public String commentInsert(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		/* 页面跳转 */
		return "/head/goods/commentInsert" ; 
	}
	
	/**
	 * 评论提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String commentInsertSubmit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		TUsers users = (TUsers) this.findObjfromSession(request, "users");
		
		TSugg comment = new TSugg();
		/* 创建对象 */
		comment.setUsersId(users.getId());
		
		ApiResponse<Object> apiResponseDb = this.goodsDbService.saveOneCommentService(comment);
		if(apiResponseDb.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
		{
			return "redirect:/head/goods/commentList.htm" ; 
		}
		return this.commentInsert(request,response);
	}

	/**
	 * 评论更新
	 * @param request
	 * @param response
	 */
	public String commentUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<TSugg> apiResponse = this.goodsDbService.findOneCommentService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/goods/commentInfo" ; 
	}
}
